package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.PurchaseEntity;
import com.softlab.retailwholesalesystem.entity.VO.PurchaseGoodsVOEntity;
import com.softlab.retailwholesalesystem.entity.StoreEntity;
import com.softlab.retailwholesalesystem.mapper.PurchaseMapper;
import com.softlab.retailwholesalesystem.mapper.StoreMapper;
import com.softlab.retailwholesalesystem.service.IDetailPurchaseService;
import com.softlab.retailwholesalesystem.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, PurchaseEntity> implements IPurchaseService {
    @Autowired
    private PurchaseMapper mapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private IDetailPurchaseService detailPurchaseService;

    @Override
    public List<PurchaseEntity> searchPurchases(PurchaseEntity purchase) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (purchase.getPurchaseId() != null) wrapper.eq("purchase_id",purchase.getPurchaseId());
        if (purchase.getName() != null) wrapper.eq("name",purchase.getName());
        if (purchase.getStatus() != null) wrapper.eq("status",purchase.getStatus());

        return mapper.selectList(wrapper);
    }

    @Override
    public void confirm(PurchaseEntity purchase) {
        Long purchaseId = purchase.getPurchaseId();
        List<PurchaseGoodsVOEntity> entities = detailPurchaseService.getMap(purchaseId);

        for (PurchaseGoodsVOEntity e:
             entities) {
            Long goodsId = e.getGoodsId();
            Long goodsNumber = e.getGoodsNumber();
            Long repositoryId = e.getRepositoryId();

            QueryWrapper<StoreEntity> storeEntityQueryWrapper = new QueryWrapper<>();
            storeEntityQueryWrapper.eq("goods_id", goodsId);
            //storeEntityQueryWrapper.eq("goods_number", goodsNumber);
            storeEntityQueryWrapper.eq("repository_id", repositoryId);
            storeEntityQueryWrapper.eq("is_delete", 0);
            List<StoreEntity> storeEntities = storeMapper.selectList(storeEntityQueryWrapper);

            if (storeEntities.isEmpty()){
                StoreEntity storeEntity = new StoreEntity();
                storeEntity.setIsDelete(0);
                storeEntity.setGoodsNumber((Integer) goodsNumber.intValue());
                storeEntity.setGoodsId((Integer) goodsId.intValue());
                storeEntity.setRepositoryId((Integer) repositoryId.intValue());
                storeMapper.insert(storeEntity);


            }else {
                //更新库存
                StoreEntity storeEntity = storeEntities.get(0);
                storeEntity.setGoodsNumber(storeEntity.getGoodsNumber() + (Integer) goodsNumber.intValue());
                storeMapper.updateById(storeEntity);

            }

        }
    }
}
